package com.administrator.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.administrator.service.ProductService;
import com.entity.Product;


/**
 * Servlet implementation class ProductEditServlet2
 */
@WebServlet("/productEditServlet2")
public class ProductEditServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductEditServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//先设置中文编码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		//获取修改页面的请求参数，然后调用业务逻辑对象，实现最终修改
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		int price=Integer.parseInt(request.getParameter("price"));
		Product p=new Product(id,name,price);
		ProductService ps=new ProductService();
		boolean result=ps.updateProduct(p);
		if(result) {
			request.getRequestDispatcher("productpage").forward(request, response);
		}else {
			response.sendRedirect("editproduct.jsp");//
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
